package cn.ffcs.uec.view.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uec.view.dao.UecArticleDao;
import cn.ffcs.uec.view.model.UecArticle;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecArticleDao")
@Transactional
public class UecArticleDaoImpl extends BaseDaoImpl implements UecArticleDao {

	@Override
	public PageInfo queryPageInfoByUecArticle(UecArticle uecArticle,
			int current, int pageSize) {
		if (null != uecArticle) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_ARTICLE T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			if (!StrUtil.isNullOrEmpty(uecArticle.getPkPsndoc())) {
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecArticle.getPkPsndoc());
			}
			sb.append(" order by T.publication_date desc ");
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecArticle.class);
		}
		return null;
	}

	@Override
	public UecArticle findUecArticleById(UecArticle uecArticle) {
		if (null != uecArticle) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_ARTICLE T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			if (!StrUtil.isNullOrEmpty(uecArticle.getArticleId())) {
				sb.append(" AND T.article_id = ? ");
				params.add(uecArticle.getArticleId());
			}
			List<UecArticle> uecEncs = this.jdbcFindList(sb.toString(), params,
					UecArticle.class);
			if (uecEncs.size() > 0) {
				return uecEncs.get(0);
			} 
		}
		return null;
	}

/**
 * 近三年情况
 */
	@Override
	public List<UecArticle> findUecArticleByPkPsndocAndDiffDate(StaffAndOutStaff staffAndOutStaff) {
		if (null != staffAndOutStaff) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_ARTICLE T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(staffAndOutStaff.getPkPsndoc());
				sb.append(" AND to_number(to_char(T.publication_date,'yyyy')) >= (Extract(year from sysdate)-3)");
				sb.append(" order by T.publication_date");		
			return this.jdbcFindList(sb.toString(), params, UecArticle.class);
		}
		return null;
	}

	@Override
	public List<UecArticle> findUecArticleByPkPsndoc(StaffAndOutStaff staffAndOutStaff) {
		if (null != staffAndOutStaff) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_ARTICLE T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			if(!StrUtil.isNullOrEmpty(staffAndOutStaff.getPkPsndoc())) {
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(staffAndOutStaff.getPkPsndoc());
				//sb.append(" AND to_number(to_char(T.publication_date,'yyyy')) >= (Extract(year from sysdate)-3)");
				sb.append(" order by T.publication_date ");	
			}
			return this.jdbcFindList(sb.toString(), params, UecArticle.class);
		}
		return null;
	}

}
